//
//  XHCreateOrderViewController.m
//  umibus
//
//  Created by fengyn on 16/6/18.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHCreateOrderViewController.h"
#import "XHUseCouponViewController.h"
#import "XHMethodPaymentViewController.h"
#import "XHVirtualAccountPaymentViewController.h"
#import "XHPaySuccessViewController.h"
#import "XHPayFailureViewController.h"
#import "XHBackRuleViewController.h"

#import "XHCreateOrderHeaderCell.h"
#import "XHCreateOrderInfoCell.h"
#import "XHCreateOrderTwoLabelCell.h"

#import "Services.h"
#import "APIKey.h"

#import <Pingpp/Pingpp.h>
#import <DateTools/DateTools.h>
///优惠方式
typedef NS_ENUM(NSUInteger, XHDiscountMethod) {
    ///不使用优惠
    XHDiscountMethodNone,
    ///积分抵扣
    XHDiscountMethodScore,
    ///优惠券抵扣
    XHDiscountMethodCoupon,
};

@interface XHCreateOrderViewController ()<XHMethodPaymentViewDelegate,XHUseCouponViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UITableView *ui_tableView;
@property (weak, nonatomic) IBOutlet UIButton *ui_nextButton;
@property (copy, nonatomic) NSString *m_submitToken;
@property (strong, nonatomic) NSDictionary *m_scoreDeductionRule;
@property (strong, nonatomic) NSDictionary *m_userInfoDic;
@property (strong, nonatomic) NSDictionary *m_score2MoneyDic;
@property (strong, nonatomic) NSDictionary *m_selectedCouponDic;
///可用的优惠券列表
@property (strong, nonatomic) NSArray *m_couponArray;
///选择的优惠方式
@property (assign, nonatomic) XHDiscountMethod m_selectedDiscount;
@end

@implementation XHCreateOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [PublicMethod setExtraCellLineHidden:self.ui_tableView];
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHCreateOrderHeaderCell" bundle:nil] forCellReuseIdentifier:XHCreateOrderHeaderCellIdentifier];
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHCreateOrderInfoCell" bundle:nil] forCellReuseIdentifier:XHCreateOrderInfoCellIdentifier];
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHCreateOrderTwoLabelCell" bundle:nil] forCellReuseIdentifier:XHCreateOrderTwoLabelCellIdentifier];
    [self.ui_tableView setBackgroundColor:[ColorManager colorWithViewBackground]];
    self.ui_nextButton.layer.cornerRadius=10;
    self.ui_nextButton.layer.masksToBounds=YES;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (self.orderDic) {
        
    }else{
        [self loadScore2MoneyData];
        [self loadSubmitTokenData];
        [self loadCouponData];
    }
    [self updateView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)loadCouponData{
    [ServiceManager getAvailableCouponsWithProduct:self.productDic[@"sequenceNBR"] price:[NSString stringWithFormat:@"%.2f",[self getOrderPrice]] success:^(NSDictionary *data) {
        if ([data isKindOfClass:[NSArray class]]) {
            self.m_couponArray=(NSArray *)data;
            [self.ui_tableView reloadData];
        }
    } failure:^(NSDictionary *data) {
        
    }];
}

- (void)updateView{
    NSString *productType=self.productDic[@"productType"];
    if (self.orderDic) {
        productType=self.orderDic[@"productType"];
    }
    if ([productType isEqualToString:kProductTypeCall]) {
        //话费直充
        self.title=@"话费直充";
    }else if ([productType isEqualToString:kProductTypeIntelligent]){
        //智能缴费
        self.title=@"智能缴费";
        UIButton *helpButton=[[UIButton alloc] init];
        helpButton.frame=CGRectMake(0, 0, 90, 30);
        [helpButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
        [helpButton setTitle:@"返还规则 " forState:UIControlStateNormal];
        [helpButton setImage:[UIImage imageNamed:@"shouye_icon_fanhuanguize"] forState:UIControlStateNormal];
        [helpButton setRightImage];
        [helpButton addTarget:self action:@selector(helpItemAction:) forControlEvents:UIControlEventTouchUpInside];
//        UIBarButtonItem *helpItem=[[UIBarButtonItem alloc] initWithTitle:@"返还规则" style:UIBarButtonItemStylePlain target:self action:@selector(helpItemAction:)];
        UIBarButtonItem *helpItem=[[UIBarButtonItem alloc] initWithCustomView:helpButton];
        self.navigationItem.rightBarButtonItem=helpItem;
    }else if ([productType isEqualToString:kProductTypeFlow]){
        //流量充值
        self.title=@"流量充值";
    }else if ([productType isEqualToString:kProductTypeFlowPackage]){
        //流量屯包
        self.title=@"流量屯包";
    }
}

- (void)helpItemAction:(id)sender{
    XHBackRuleViewController *viewController=[[XHBackRuleViewController alloc] init];
    viewController.startDate=[NSDate date];
    if (self.orderDic) {
        viewController.count=[self.orderDic[@"packageCount"] intValue];
        viewController.amount=[self.orderDic[@"productValue"] doubleValue];
    }else{
        viewController.count=[self.productDic[@"packageCount"] intValue];
        viewController.amount=[self.productDic2[@"productValue"] doubleValue];
    }
    viewController.modalPresentationStyle=UIModalPresentationCustom;
    [self presentViewController:viewController animated:NO completion:nil];
}

- (void)loadUserInfoData{
    self.m_userInfoDic=[ServiceManager getUserInfoSuccess:^(NSDictionary *data) {
        if ([data isKindOfClass:[NSDictionary class]]) {
            self.m_userInfoDic=data;
            [self.ui_tableView reloadData];
        }
    } failure:^{
        
    }];
    [self.ui_tableView reloadData];
}

- (void)loadScore2MoneyData{
    NSString *price=[NSString stringWithFormat:@"%.2f",[self getOrderPrice]];
    [ServiceManager getScore2MoneyWithPrice:price productType:self.productDic[@"productType"] success:^(NSDictionary *data) {
        if ([data isKindOfClass:[NSDictionary class]]) {
            self.m_score2MoneyDic=data;
            [self.ui_tableView reloadData];
        }
    } failure:^(NSDictionary *data) {
        
    }];
}

- (void)loadScoreEventData{
    [ServiceManager getScoreDeductionRuleSuccess:^(NSDictionary *data) {
        if ([data isKindOfClass:[NSDictionary class]]) {
            self.m_scoreDeductionRule=data;
            [self.ui_tableView reloadData];
        }
    } failure:^(NSDictionary *data) {
        
    }];
}

- (void)loadSubmitTokenData{
    [ServiceManager getSubmitTokenSuccess:^(NSDictionary *data) {
        if ([data isKindOfClass:[NSString class]]) {
            self.m_submitToken=(NSString *)data;
        }
    } failure:^(NSDictionary *data) {
        
    }];
}

- (IBAction)nextButtonAction:(id)sender {
    XHMethodPaymentViewController *viewController=[[XHMethodPaymentViewController alloc] init];
    viewController.delegate=self;
    NSString * amount=[NSString stringWithFormat:@"%.2f",[self calcWillPayMoney]];
    viewController.m_integral =amount;
    viewController.modalPresentationStyle=UIModalPresentationCustom;
    [self presentViewController:viewController animated:NO completion:nil];
}

- (double)getOrderPrice{
    double amount=0;
    NSString *productType=self.productDic[@"productType"];
    if ([productType isEqualToString:kProductTypeCall]) {
        //话费直充
        if ([self.productDic[@"isPromotion"] isEqualToString:@"Y"]) {
            amount=[self.productDic[@"promotionPrice"] doubleValue];
        }else
        {
            amount=[self.productDic[@"salesPrice"] doubleValue];
        }
    }else if ([productType isEqualToString:kProductTypeIntelligent]){
        //智能缴费
        //月数
        int count=[self.productDic[@"packageCount"] intValue];
        //折扣
        double discount = [self.productDic[@"packageDiscount"] doubleValue];
        //单价
        double price = [self.productDic2[@"productValue"] doubleValue];
        amount= price * count * discount/10;
        
    }else if ([productType isEqualToString:kProductTypeFlow]){
        //流量充值
        if ([self.productDic[@"isPromotion"] isEqualToString:@"Y"]) {
            amount=[self.productDic[@"promotionPrice"] doubleValue];
        }else
        {
            amount=[self.productDic[@"salesPrice"] doubleValue];
        }
    }else if ([productType isEqualToString:kProductTypeFlowPackage]){
        //流量屯包
        //月数
        int count=[self.productDic[@"packageCount"] intValue];
        //折扣
        double discount = [self.productDic[@"packageDiscount"] doubleValue];
        //单价
        double price = [self.productDic[@"salesPrice"] doubleValue];
        amount= price * count * discount/10;
    }
    if (self.orderDic) {
        amount=[self.orderDic[@"amount"] doubleValue];
    }
    return amount;
}

- (double)calcWillPayMoney{
    double amount=[self getOrderPrice];
    if (self.m_selectedDiscount==XHDiscountMethodScore) {
        if (self.m_score2MoneyDic) {
            double price=[self.m_score2MoneyDic[@"price"] doubleValue];
            amount=amount-price;
        }
    }else if(self.m_selectedDiscount==XHDiscountMethodCoupon)
    {
        if (self.m_selectedCouponDic) {
            amount=amount-[self calcCouponPrice];
        }
    }
    if (self.orderDic) {
        amount=[self.orderDic[@"amount"] doubleValue];
    }
    return amount;
}

- (double)calcCouponPrice{
    double price=0;
    if (self.m_selectedCouponDic) {
        NSDictionary *dic=self.m_selectedCouponDic;
        if([dic[@"couponType"]isEqualToString:@"VOUCHER_TICKET"])
        {
            //代金券
            price=[dic[@"couponValue"] doubleValue];
        }else
        {
            double amount=[self getOrderPrice];
            double discount=[dic[@"couponValue"] doubleValue];
            price= amount-(amount * discount/10);
        }
    }
    return price;
}

- (void)goPaySuccessView{
    //去支付成功界面;
    XHPaySuccessViewController *viewController=[[XHPaySuccessViewController alloc] init];
    viewController.amount=[self calcWillPayMoney];
    viewController.productType=self.productDic[@"productType"];
    if (self.orderDic) {
        viewController.productType=self.orderDic[@"orderType"];
    }
    [self.navigationController pushViewController:viewController animated:YES];
}

- (void)goPayFailureView{
    //去支付失败界面;
    XHPayFailureViewController *viewController=[[XHPayFailureViewController alloc] init];
    [self.navigationController pushViewController:viewController animated:YES];
}

- (void)submitActionWithPayMethod:(NSString *)payMethod{
    WS(weakSelf)
    if (self.orderDic) {
        BOOL isIntelligent=NO;
        if ([self.orderDic[@"orderType"] isEqualToString:kProductTypeIntelligent]) {
            isIntelligent=YES;
        }
        [ServiceManager repayOrderWithOrderSn:self.orderDic[@"orderSn"] payMethod:payMethod password:@"" isIntelligent:isIntelligent success:^(NSDictionary *data) {
            NSString *charge=[NSString jsonStringWithNSDictionary:data];
            [Pingpp createPayment:charge viewController:weakSelf appURLScheme:appUrlScheme withCompletion:^(NSString *result, PingppError *error) {
                NSLog(@"completion block: %@", result);
                if (error == nil) {
                    NSLog(@"PingppError is nil");
                } else {
                    NSLog(@"PingppError: code=%lu msg=%@", (unsigned  long)error.code, [error getMsg]);
                }
                if ([result isEqualToString:@"success"]) {
                    [ServiceManager payFinishWithChargeId:data[@"id"] success:^(NSDictionary *data) {
                        
                    } failure:^(NSDictionary *data) {
                        
                    }];
                    [weakSelf goPaySuccessView];
                }else
                {
                    [weakSelf goPayFailureView];
                }
            }];
        } failure:^(NSDictionary *data) {
            
        }];
        return;
    }
    NSString *score=@"";
    NSString *couponNo=@"";
    NSString *phoneNumber=@"";
    NSString *phoneLocation=@"";
    if (self.m_selectedDiscount==XHDiscountMethodScore) {
        if (self.m_score2MoneyDic) {
            score=self.m_score2MoneyDic[@"score"];
        }
    }else{
        if (self.m_selectedCouponDic) {
            couponNo=self.m_selectedCouponDic[@"sequenceNBR"];
        }
    }
    NSDictionary *product=@{self.productDic[@"sequenceNBR"]:@1};
    if (self.productDic2) {
        product=@{self.productDic[@"sequenceNBR"]:@1,
                  self.productDic2[@"sequenceNBR"]:self.productDic[@"packageCount"]};
    }
    if (self.phoneNumber) {
        phoneNumber=self.phoneNumber;
    }
    if (self.operatorDic) {
        phoneLocation=self.operatorDic[@"provinceID"];
    }
    NSString *amount=[NSString stringWithFormat:@"%.2f",[self calcWillPayMoney]];
    [ServiceManager createOrderWithProductType:self.productDic[@"productType"] amount:amount payMethod:payMethod products:product password:@"" phoneLocation:phoneLocation phoneNumber:phoneNumber submitToken:self.m_submitToken score:score couponNo:couponNo success:^(NSDictionary *data) {
        NSString *charge=[NSString jsonStringWithNSDictionary:data];
        [Pingpp createPayment:charge viewController:weakSelf appURLScheme:appUrlScheme withCompletion:^(NSString *result, PingppError *error) {
            NSLog(@"completion block: %@", result);
            if (error == nil) {
                NSLog(@"PingppError is nil");
            } else {
                NSLog(@"PingppError: code=%lu msg=%@", (unsigned  long)error.code, [error getMsg]);
            }
            if ([result isEqualToString:@"success"]) {
                //                [[NSNotificationCenter defaultCenter] postNotificationName:paySuccessNotification object:nil];
                [ServiceManager payFinishWithChargeId:data[@"id"] success:^(NSDictionary *data) {
                    
                } failure:^(NSDictionary *data) {
                    
                }];
                [weakSelf goPaySuccessView];
            }else
            {
                //                [[NSNotificationCenter defaultCenter] postNotificationName:payFailureNotification object:nil];
                [weakSelf goPayFailureView];
            }
        }];
    } failure:^(NSDictionary *data) {
        
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableVie
{
    NSInteger number=3;
    if (self.orderDic) {
        number=2;
    }
    return number;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger rows=0;
    switch (section) {
        case 0:
            rows=1;
            break;
        case 1:
            rows=1;
            break;
        case 2:
        {
            NSString *productType=self.productDic[@"productType"];
            if (self.orderDic) {
                productType=self.orderDic[@"orderType"];
            }
            if ([productType isEqualToString:kProductTypeFlowPackage]) {
                rows=2;
            }else{
                rows=3;
            }
        }
            break;
    }
    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=nil;
    switch (indexPath.section) {
        case 0:
            cell=[self configHeaderCell];
            break;
        case 1:
            cell=[self configInfoCell];
            break;
        case 2:
            cell=[self configCellWithIndexPath:indexPath];
            break;
    }
    return cell;
}

- (UITableViewCell *)configHeaderCell{
    XHCreateOrderHeaderCell *cell=[self.ui_tableView dequeueReusableCellWithIdentifier:XHCreateOrderHeaderCellIdentifier];
    NSString *productType=self.productDic[@"productType"];
    NSString *operatorCode=self.productDic[@"operatorCode"];
    NSString *phoneNumber=self.phoneNumber;
    if (self.orderDic) {
        productType=self.orderDic[@"orderType"];
        operatorCode=self.orderDic[@"operatorCode"];
        phoneNumber=self.orderDic[@"orderPostscript"];
    }
    
    cell.ui_valueLabel.text=[NSString stringWithFormat:@"%.2f",[self calcWillPayMoney]];
    if (phoneNumber) {
        cell.ui_phoneLabel.text=[NSString stringWithFormat:@"手机号码:%@",phoneNumber];
        cell.ui_phoneLabel.hidden=NO;
        cell.ui_lineView.hidden=NO;
    }else{
        cell.ui_phoneLabel.hidden=YES;
        cell.ui_lineView.hidden=YES;
    }
    
    if ([operatorCode isEqualToString:kOperatorCodeCMCC]) {
        cell.ui_imageView.image=[UIImage imageNamed:@"shouye_icon_yidong"];
    }else if ([operatorCode isEqualToString:kOperatorCodeChinaUnicom]) {
        cell.ui_imageView.image=[UIImage imageNamed:@"shouye_icon_liantong"];
    }else if ([operatorCode isEqualToString:kOperatorCodeChinaNet]) {
        cell.ui_imageView.image=[UIImage imageNamed:@"shouye_icon_dianxin"];
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return cell;
}

- (UITableViewCell *)configInfoCell{
    XHCreateOrderInfoCell *cell=[self.ui_tableView dequeueReusableCellWithIdentifier:XHCreateOrderInfoCellIdentifier];
    NSString *productType=self.productDic[@"productType"];
    if (self.orderDic) {
        productType=self.orderDic[@"orderType"];
    }
    NSDate *date=[NSDate date];
    cell.ui_leftLabel1.text=[date formattedDateWithFormat:@"yyyy/MM/dd"];
    if ([productType isEqualToString:kProductTypeCall]) {
        cell.ui_leftLabel5.hidden=YES;
        cell.ui_leftLabel6.hidden=YES;
        cell.ui_rightLabel5.hidden=YES;
        cell.ui_rightLabel6.hidden=YES;
        
        cell.ui_leftLabel2.text=@"商品名称";
        cell.ui_leftLabel3.text=@"原价";
        cell.ui_leftLabel4.text=@"订单价";
        
        cell.ui_rightLabel1.text=@"话费充值";
        cell.ui_rightLabel2.text=[NSString stringWithFormat:@"%@￥%@",self.productDic[@"operatorName"],self.productDic[@"productValue"]];
        cell.ui_rightLabel3.text=[NSString stringWithFormat:@"￥%.2f",[self.productDic[@"marketPrice"] doubleValue]];
        cell.ui_rightLabel4.text=[NSString stringWithFormat:@"￥%.2f",[self getOrderPrice]];
        if (self.orderDic) {
            cell.ui_rightLabel2.text=[NSString stringWithFormat:@"%@￥%@",self.orderDic[@"operatorName"],self.orderDic[@"productValue"]];
            cell.ui_rightLabel3.text=[NSString stringWithFormat:@"￥%.2f",[self.orderDic[@"marketPrice"] doubleValue]];
            cell.ui_rightLabel4.text=[NSString stringWithFormat:@"￥%.2f",[self getOrderPrice]];
        }
        
    }else if ([productType isEqualToString:kProductTypeIntelligent]){
        cell.ui_leftLabel5.hidden=NO;
        cell.ui_rightLabel5.hidden=NO;
        cell.ui_leftLabel6.hidden=YES;
        cell.ui_rightLabel6.hidden=YES;
        
        cell.ui_leftLabel2.text=@"商品名称";
        cell.ui_leftLabel3.text=@"商品数量";
        cell.ui_leftLabel4.text=@"原价";
        cell.ui_leftLabel5.text=@"订单价";
        
        cell.ui_rightLabel1.text=@"智能缴费";
        cell.ui_rightLabel2.text=[NSString stringWithFormat:@"%@￥%@",self.productDic[@"operatorName"],self.productDic2[@"productValue"]];
        cell.ui_rightLabel3.text=[NSString stringWithFormat:@"%@个",self.productDic[@"packageCount"]];
        //月数
        int count=[self.productDic[@"packageCount"] intValue];
        //折扣
        double discount = [self.productDic[@"packageDiscount"] doubleValue];
        //单价
        double price = [self.productDic2[@"marketPrice"] doubleValue];
        if (self.orderDic) {
            cell.ui_rightLabel2.text=[NSString stringWithFormat:@"%@￥%@",self.orderDic[@"operatorName"],self.orderDic[@"productValue"]];
            cell.ui_rightLabel3.text=[NSString stringWithFormat:@"%@个",self.orderDic[@"packageCount"]];
            count=[self.orderDic[@"packageCount"] intValue];
            discount = [self.orderDic[@"packageDiscount"] doubleValue];
            price = [self.orderDic[@"marketPrice"] doubleValue];
        }
        
        cell.ui_rightLabel3.text=[NSString stringWithFormat:@"%d个",count];
        cell.ui_rightLabel4.text=[NSString stringWithFormat:@"￥%.2f",price*count];
        cell.ui_rightLabel5.text=[NSString stringWithFormat:@"￥%.2f",[self getOrderPrice]];
    }else if ([productType isEqualToString:kProductTypeFlow]){
        cell.ui_leftLabel5.hidden=YES;
        cell.ui_rightLabel5.hidden=YES;
        cell.ui_leftLabel6.hidden=YES;
        cell.ui_rightLabel6.hidden=YES;
        cell.ui_leftLabel2.text=@"商品名称";
        cell.ui_leftLabel3.text=@"原价";
        cell.ui_leftLabel4.text=@"订单价";
        
        cell.ui_rightLabel1.text=@"流量充值";
        NSInteger flowCount=[self.productDic[@"productValue"] integerValue];
        if (flowCount<1000) {
            cell.ui_rightLabel2.text=[NSString stringWithFormat:@"%@ %@M",self.productDic[@"operatorName"],self.productDic[@"productValue"]];
        }else{
            float gcount=flowCount/(float)1000;
            NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
            formatter.numberStyle = NSNumberFormatterRoundFloor;
            cell.ui_rightLabel2.text=[NSString stringWithFormat:@"%@ %@G",self.productDic[@"operatorName"],[formatter stringFromNumber:@(gcount)]];
        }
        
        cell.ui_rightLabel3.text=[NSString stringWithFormat:@"￥%.2f",[self.productDic[@"marketPrice"] doubleValue]];
        cell.ui_rightLabel4.text=[NSString stringWithFormat:@"￥%.2f",[self getOrderPrice]];
        if (self.orderDic) {
            NSInteger flowCount=[self.orderDic[@"productValue"] integerValue];
            if (flowCount<1000) {
                cell.ui_rightLabel2.text=[NSString stringWithFormat:@"%@ %@M",self.orderDic[@"operatorName"],self.orderDic[@"productValue"]];
            }else{
                float gcount=flowCount/(float)1000;
                NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
                formatter.numberStyle = NSNumberFormatterRoundFloor;
                cell.ui_rightLabel2.text=[NSString stringWithFormat:@"%@ %@G",self.orderDic[@"operatorName"],[formatter stringFromNumber:@(gcount)]];
            }
            cell.ui_rightLabel3.text=[NSString stringWithFormat:@"￥%.2f",[self.orderDic[@"marketPrice"] doubleValue]];
            cell.ui_rightLabel4.text=[NSString stringWithFormat:@"￥%.2f",[self getOrderPrice]];
        }
    }else if ([productType isEqualToString:kProductTypeFlowPackage]){
        cell.ui_leftLabel5.hidden=NO;
        cell.ui_leftLabel6.hidden=NO;
        cell.ui_rightLabel5.hidden=NO;
        cell.ui_rightLabel6.hidden=NO;
        
        cell.ui_leftLabel2.text=@"商品名称";
        cell.ui_leftLabel3.text=@"商品数量";
        cell.ui_leftLabel4.text=@"单包原价";
        cell.ui_leftLabel5.text=@"屯包原价";
        cell.ui_leftLabel6.text=@"订单价";
        
        cell.ui_rightLabel1.text=@"流量屯包";
        NSInteger flowCount=[self.productDic[@"productValue"] integerValue];
        if (flowCount<1000) {
            cell.ui_rightLabel2.text=[NSString stringWithFormat:@"%@ %@M",self.productDic[@"operatorName"],self.productDic[@"productValue"]];
        }else{
            float gcount=flowCount/(float)1000;
            NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
            formatter.numberStyle = NSNumberFormatterRoundFloor;
            cell.ui_rightLabel2.text=[NSString stringWithFormat:@"%@ %@G",self.productDic[@"operatorName"],[formatter stringFromNumber:@(gcount)]];
        }
        cell.ui_rightLabel3.text=[NSString stringWithFormat:@"%@件装",self.productDic[@"packageCount"]];
        cell.ui_rightLabel4.text=[NSString stringWithFormat:@"%@￥%@",self.productDic[@"productValue"],self.productDic[@"productValue"]];
        cell.ui_rightLabel4.text=[NSString stringWithFormat:@"￥%.2f",[self.productDic[@"marketPrice"] doubleValue]];
        if ([self.productDic[@"isPromotion"] isEqualToString:@"Y"]) {
            cell.ui_rightLabel5.text=[NSString stringWithFormat:@"￥%.2f",[self.productDic[@"promotionPrice"] doubleValue]*[self.productDic[@"packageCount"] intValue]];
        }else
        {
            cell.ui_rightLabel5.text=[NSString stringWithFormat:@"￥%.2f",[self.productDic[@"salesPrice"] doubleValue]*[self.productDic[@"packageCount"] intValue]];
        }
        cell.ui_rightLabel6.text=[NSString stringWithFormat:@"￥%.2f",[self getOrderPrice]];
        if (self.orderDic) {
            NSInteger flowCount=[self.orderDic[@"productValue"] integerValue];
            if (flowCount<1000) {
                cell.ui_rightLabel2.text=[NSString stringWithFormat:@"%@ %@M",self.orderDic[@"operatorName"],self.orderDic[@"productValue"]];
            }else{
                float gcount=flowCount/(float)1000;
                NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
                formatter.numberStyle = NSNumberFormatterRoundFloor;
                cell.ui_rightLabel2.text=[NSString stringWithFormat:@"%@ %@G",self.orderDic[@"operatorName"],[formatter stringFromNumber:@(gcount)]];
            }
            cell.ui_rightLabel3.text=[NSString stringWithFormat:@"%@件装",self.orderDic[@"packageCount"]];
            cell.ui_rightLabel4.text=[NSString stringWithFormat:@"%@￥%@",self.productDic[@"productValue"],self.orderDic[@"productValue"]];
            cell.ui_rightLabel4.text=[NSString stringWithFormat:@"￥%.2f",[self.orderDic[@"marketPrice"] doubleValue]];
            if ([self.orderDic[@"isPromotion"] isEqualToString:@"Y"]) {
                cell.ui_rightLabel5.text=[NSString stringWithFormat:@"￥%.2f",[self.orderDic[@"promotionPrice"] doubleValue]*[self.orderDic[@"packageCount"] intValue]];
            }else
            {
                cell.ui_rightLabel5.text=[NSString stringWithFormat:@"￥%.2f",[self.orderDic[@"salesPrice"] doubleValue]*[self.orderDic[@"packageCount"] intValue]];
            }
        }
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return cell;
}

- (UITableViewCell *)configCellWithIndexPath:(NSIndexPath *)indexPath{
    XHCreateOrderTwoLabelCell *cell=[self.ui_tableView dequeueReusableCellWithIdentifier:XHCreateOrderTwoLabelCellIdentifier];
    cell.la_leftLabelConstraintLeft.constant=34;
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    switch (indexPath.row) {
        case 0:
        {
            cell.ui_leftLabel.text=@"优惠";
            cell.ui_rightLabel.hidden=NO;
            cell.ui_rightLabel.text=@"";
            cell.ui_button.hidden=YES;
            cell.la_leftLabelConstraintLeft.constant=8;
            cell.accessoryType=UITableViewCellAccessoryNone;
        }
            break;
        case 1:
        {
            NSString *productType=self.productDic[@"productType"];
            if (self.orderDic) {
                productType=self.orderDic[@"orderType"];
            }
            if ([productType isEqualToString:kProductTypeFlowPackage]) {
                ///选择优惠券
                cell.ui_button.selected=self.m_selectedDiscount==XHDiscountMethodCoupon;
                cell.ui_leftLabel.text=@"选择优惠券";
                cell.ui_rightLabel.hidden=NO;
                if (self.m_selectedCouponDic) {
                    cell.ui_rightLabel.text=[NSString stringWithFormat:@"-￥%.2f",[self calcCouponPrice]];
                }else{
                    cell.ui_rightLabel.text=@"";
                }
                cell.ui_button.hidden=YES;
                cell.la_leftLabelConstraintLeft.constant=8;
            }else{
                ///积分抵扣
                cell.ui_button.selected=self.m_selectedDiscount==XHDiscountMethodScore;
                if (self.m_score2MoneyDic) {
                    cell.ui_leftLabel.text=[NSString stringWithFormat:@"可使用%@积分抵扣%.2f元",self.m_score2MoneyDic[@"score"],[self.m_score2MoneyDic[@"price"] doubleValue]];
                }else{
                    cell.ui_leftLabel.text=[NSString stringWithFormat:@"可使用%@积分抵扣%@元",@"0",@"0"];
                }
                cell.ui_rightLabel.hidden=YES;
            }
        }
            break;
        case 2:
        {
            ///选择优惠券
            cell.ui_button.selected=self.m_selectedDiscount==XHDiscountMethodCoupon;
            cell.ui_leftLabel.text=@"选择优惠券";
            cell.ui_rightLabel.hidden=NO;
            if (self.m_selectedCouponDic) {
                cell.ui_rightLabel.text=[NSString stringWithFormat:@"-￥%.2f",[self calcCouponPrice]];
            }else{
                cell.ui_rightLabel.text=@"";
            }
        }
            break;
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height=50;
    switch (indexPath.section) {
        case 0:
        {
            height=170;
            NSString *phoneNumber=self.phoneNumber;
            if (self.orderDic) {
                phoneNumber=self.orderDic[@"orderPostscript"];
            }
            if (phoneNumber) {
                height=170;
            }else{
                height=120;
            }
        }
            break;
        case 1:
        {
            NSString *productType=self.productDic[@"productType"];
            if (self.orderDic) {
                productType=self.orderDic[@"productType"];
            }
            if ([productType isEqualToString:kProductTypeCall]) {
                height=310/2;
            }else if ([productType isEqualToString:kProductTypeIntelligent]){
                height=364/2;
            }else if ([productType isEqualToString:kProductTypeFlow]){
                height=310/2;
            }else if ([productType isEqualToString:kProductTypeFlowPackage]){
                height=414/2;
            }
            
        }
            break;
        case 2:
            height=50;
            break;
    }
    return height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0000001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.section) {
        case 0:
        {
        }
            break;
        case 1:
        {
        }
            break;
        case 2:
        {
            switch (indexPath.row) {
                case 0:
                {
                }
                    break;
                case 1:
                {
                    NSString *productType=self.productDic[@"productType"];
                    if (self.orderDic) {
                        productType=self.orderDic[@"orderType"];
                    }
                    if ([productType isEqualToString:kProductTypeFlowPackage]) {
                        if ([self.m_couponArray count]==0) {
                            [PromptAction showInfoPrompt:@"无可用优惠券"];
                            return;
                        }
                        if (self.m_selectedDiscount==XHDiscountMethodCoupon) {
                            self.m_selectedDiscount=XHDiscountMethodNone;
                            self.m_selectedCouponDic=nil;
                            [self.ui_tableView reloadData];
                            return;
                        }
                        ///选择优惠券
                        self.m_selectedDiscount=XHDiscountMethodCoupon;
                        XHUseCouponViewController *viewController=[[XHUseCouponViewController alloc] init];
                        viewController.delegate=self;
                        viewController.productDic=self.productDic;
                        viewController.selectedDic=self.m_selectedCouponDic;
                        viewController.orderPrice=[self getOrderPrice];
                        viewController.modalPresentationStyle=UIModalPresentationCustom;
                        [self presentViewController:viewController animated:NO completion:nil];
                        [self.ui_tableView reloadData];
                    }else{
                        if ([self.m_score2MoneyDic[@"price"] doubleValue]<=0) {
                            [PromptAction showInfoPrompt:@"无可用积分"];
                            return;
                        }
                        if (self.m_selectedDiscount==XHDiscountMethodScore) {
                            self.m_selectedDiscount=XHDiscountMethodNone;
                            [self.ui_tableView reloadData];
                            return;
                        }
                        ///积分抵扣
                        self.m_selectedDiscount=XHDiscountMethodScore;
                        [self.ui_tableView reloadData];
                    }
                }
                    break;
                case 2:
                {
                    if ([self.m_couponArray count]==0) {
                        [PromptAction showInfoPrompt:@"无可用优惠券"];
                        return;
                    }
                    if (self.m_selectedDiscount==XHDiscountMethodCoupon) {
                        self.m_selectedDiscount=XHDiscountMethodNone;
                        self.m_selectedCouponDic=nil;
                        [self.ui_tableView reloadData];
                        return;
                    }
                    self.m_selectedDiscount=XHDiscountMethodCoupon;
                    XHUseCouponViewController *viewController=[[XHUseCouponViewController alloc] init];
                    viewController.delegate=self;
                    viewController.productDic=self.productDic;
                    viewController.selectedDic=self.m_selectedCouponDic;
                    viewController.orderPrice=[self getOrderPrice];
                    viewController.modalPresentationStyle=UIModalPresentationCustom;
                    [self presentViewController:viewController animated:NO completion:nil];
                    [self.ui_tableView reloadData];
                }
                    break;
            }
        }
            break;
    }
}

#pragma mark - XHMethodPaymentViewDelegate
- (void)didSelectedMethodPayment:(NSString *)payMethod{
    if ([payMethod isEqualToString:kPayChannelYE]) {
        XHVirtualAccountPaymentViewController *viewController=[[XHVirtualAccountPaymentViewController alloc] init];
        viewController.productDic=self.productDic;
        viewController.productDic2=self.productDic2;
        viewController.operatorDic=self.operatorDic;
        viewController.phoneNumber=self.phoneNumber;
        viewController.m_submitToken=self.m_submitToken;
        viewController.amount=[self calcWillPayMoney];
        viewController.orderDic=self.orderDic;
        NSString *score=@"";
        NSString *couponNo=@"";
        if (self.m_selectedDiscount==XHDiscountMethodScore) {
            if (self.m_score2MoneyDic) {
                score=self.m_score2MoneyDic[@"score"];
            }
        }else{
            if (self.m_selectedCouponDic) {
                couponNo=self.m_selectedCouponDic[@"sequenceNBR"];
            }
        }
        viewController.score=score;
        viewController.couponNo=couponNo;
        [self.navigationController pushViewController:viewController animated:YES];
    }else{
        [self submitActionWithPayMethod:payMethod];
    }
}

#pragma mark - XHUseCouponViewControllerDelegate
- (void)didSelectedCoupon:(NSDictionary *)coupon{
    self.m_selectedCouponDic=coupon;
    [self.ui_tableView reloadData];
}
@end
